package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.Equipamento;

public class EquipamentoCRUD implements IEquipamentoDAO {

	static PreparedStatement stm;
	static ResultSet rs;

	public void deletarEquipamento(Equipamento equip)
			throws ClassNotFoundException, SQLException {
		stm = ConectaBanco.getConexaoMySQL().prepareStatement(
				"delete from equipamento  where num_serie = ?");

		stm.setInt(1, equip.getNumSerie());

		stm.executeUpdate();

		stm.close();

	}

	public void inserirEquipamento(Equipamento equip)
			throws ClassNotFoundException, SQLException {

		stm = ConectaBanco
				.getConexaoMySQL()
				.prepareStatement(
						"insert into equipamento (num_serie,marca,tipo,garantia,id_setor) values (?,?,?,?,?)");

		stm.setInt(1, equip.getNumSerie());
		stm.setString(2, equip.getMarca());
		stm.setString(3, equip.getTipo());
		stm.setString(4, equip.getGarantia());
		stm.setInt(5, equip.getIdSetor());

		System.out.println(stm.toString());

		stm.execute();

	}

	public ArrayList<Equipamento> listarEquipamento() throws SQLException,
			ClassNotFoundException, NullPointerException {
		ArrayList<Equipamento> listaEquipamento = new ArrayList<Equipamento>();
		Equipamento equipTmp;

		stm = ConectaBanco.getConexaoMySQL().prepareStatement(
				"select * from equipamento");
		rs = stm.executeQuery();
		while (rs.next()) {
			equipTmp = new Equipamento(rs.getInt("num_serie"),
					rs.getString("marca"), rs.getString("tipo"),
					rs.getString("garantia"), rs.getInt("id_setor"));
			listaEquipamento.add(equipTmp);

		}
		stm.close();
		rs.close();
		return listaEquipamento;
	}

	@Override
	public void alterarEquipamento(Equipamento altEquip)
			throws ClassNotFoundException, SQLException {

		stm = ConectaBanco
				.getConexaoMySQL()
				.prepareStatement(
						"update equipamento set marca = ?, tipo = ?, garantia = ?, id_setor = ?  where num_serie = ?");

		stm.setString(1, altEquip.getMarca());
		stm.setString(2, altEquip.getTipo());
		stm.setString(3, altEquip.getGarantia());
		stm.setInt(4, altEquip.getIdSetor());
		stm.setInt(5, altEquip.getNumSerie());

		stm.executeUpdate();

		stm.close();

	}

}
